{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "import mmengine\n",
    "from mmdet3d.structures import LiDARInstance3DBoxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# info_path = '../data/nuscenes/nuscenes_infos_train.pkl'\n",
    "info_path = '../data/CODA/_coda_infos_val.pkl'\n",
    "# info_path = '../data/CODA/coda_16lines_infos_val.pkl'\n",
    "# info_path = '../data/wheelchair_infos_train.pkl'\n",
    "# info_path = '../data/CODA/detection_results.pkl'\n",
    "info = mmengine.load(info_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "563"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(info['data_list'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'10_9200'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info['data_list'][200]['token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "info_path = '../data/CODA/coda_16lines_infos_val.pkl'\n",
    "info2 = mmengine.load(info_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'categories': {'Pedestrian': 0, 'Cyclist': 1},\n",
       " 'dataset': 'wheelchair',\n",
       " 'version': 'v1.0',\n",
       " 'split': 'train'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info['metainfo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'categories': {'Car': 0, 'Pedestrian': 1, 'Cyclist': 2},\n",
       " 'dataset': 'coda',\n",
       " 'version': 'v1.0',\n",
       " 'split': 'test'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info2['metainfo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "861"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(info['data_list'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "281"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(info2['data_list'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['scene', 'sequence', 'frame', 'sample_idx', 'token', 'lidar_points', 'images', 'instances'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info['data_list'][0].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['scene', 'frame', 'sample_idx', 'token', 'timestamp', 'lidar_points', 'images', 'instances', 'ego2global'])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info2['data_list'][0].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'bbox_label_3d': 1,\n",
       "  'bbox_3d': [1.1809, 1.9624, 0.1293, 1.7982, 0.8, 1.8108, 0.3167],\n",
       "  'instance_id': '27'},\n",
       " {'bbox_label_3d': 1,\n",
       "  'bbox_3d': [5.8357, 2.8655, 0.5213, 1.7982, 0.8, 1.8053, 0.2888],\n",
       "  'instance_id': '28'},\n",
       " {'bbox_label_3d': 0,\n",
       "  'bbox_3d': [14.0359, 1.8315, 1.36, 0.4045, 0.7003, 1.7488, -0.0167],\n",
       "  'instance_id': '29'},\n",
       " {'bbox_label_3d': 0,\n",
       "  'bbox_3d': [14.3904, 1.3455, 1.422, 0.5789, 0.7003, 1.8, 1.4322],\n",
       "  'instance_id': '30'},\n",
       " {'bbox_label_3d': 0,\n",
       "  'bbox_3d': [1.2847, -5.8131, 0.5026, 0.627, 0.8, 1.8, 0.1866],\n",
       "  'instance_id': '31'}]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info['data_list'][501]['instances']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['lidar_points', 'timestamp', 'lidar_sweeps', 'images', 'box_type_3d', 'box_mode_3d'])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info['box_mode_3d']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['sample_idx', 'pre_bboxes', 'pre_labels', 'pre_scores'])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['metainfo']['categories']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class_names = ['Pedestrian', 'Cyclist']\n",
    "class_labels = [info['metainfo']['categories'][class_name] for class_name in class_names]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['pre_bboxes'][10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['metainfo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['data_list'][10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['data_list'][10]['instances']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['data_list'][-1]['ego2global']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(info['data_list'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instance_range = [np.inf, np.inf, np.inf, -np.inf, -np.inf, -np.inf]\n",
    "corners = []\n",
    "for sample in info['data_list']:\n",
    "    bboxes = [instance['bbox_3d'] for instance in sample['instances']]\n",
    "    bboxes_3d = LiDARInstance3DBoxes(bboxes, origin=(0.5, 0.5, 0.5))\n",
    "    corner = bboxes_3d.corners.reshape(-1, 3).numpy()\n",
    "    corners.append(np.concatenate([corner.min(axis=0), corner.max(axis=0)]))\n",
    "    instance_range[0:3] = np.minimum(instance_range[0:3], np.min(corner, axis=0))\n",
    "    instance_range[3:6] = np.maximum(instance_range[3:6], np.max(corner, axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sum = 0\n",
    "distribute = [0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
    "for sample in info['data_list']:    \n",
    "    for instance in sample['instances']:\n",
    "        if instance['bbox_label_3d'] == 2:\n",
    "            sum += 1\n",
    "            if instance['bbox_3d'][5] < 1.0:\n",
    "                distribute[0] += 1\n",
    "            elif instance['bbox_3d'][5] < 1.1:\n",
    "                distribute[1] += 1\n",
    "            elif instance['bbox_3d'][5] < 1.2:\n",
    "                distribute[2] += 1\n",
    "            elif instance['bbox_3d'][5] < 1.3:\n",
    "                distribute[3] += 1\n",
    "            elif instance['bbox_3d'][5] < 1.4:\n",
    "                distribute[4] += 1\n",
    "            elif instance['bbox_3d'][5] < 1.5:\n",
    "                distribute[5] += 1\n",
    "                print(instance['bbox_3d'], sample['token'])\n",
    "            elif instance['bbox_3d'][5] < 1.6:\n",
    "                distribute[6] += 1\n",
    "            elif instance['bbox_3d'][5] < 1.7:\n",
    "                distribute[7] += 1\n",
    "            else:\n",
    "                distribute[8] += 1\n",
    "print(sum, distribute)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "corners"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instance_range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['data_list'][10]['instances']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info['data_list'][0]['images']['cam1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BBOX_CLASS_TO_ID = {\n",
    "    # Dynamic Classes\n",
    "    \"Car\"                   : 0,\n",
    "    \"Pedestrian\"            : 1,\n",
    "    \"Bike\"                  : 2,\n",
    "    \"Motorcycle\"            : 3,\n",
    "    \"Golf Cart\"             : 4, # Unused\n",
    "    \"Truck\"                 : 5, # Unused\n",
    "    \"Scooter\"               : 6,\n",
    "    # Static Classes\n",
    "    \"Tree\"                  : 7,\n",
    "    \"Traffic Sign\"          : 8,\n",
    "    \"Canopy\"                : 9,\n",
    "    \"Traffic Light\"         : 10,\n",
    "    \"Bike Rack\"             : 11,\n",
    "    \"Bollard\"               : 12,\n",
    "    \"Construction Barrier\"  : 13, # Unused\n",
    "    \"Parking Kiosk\"         : 14,\n",
    "    \"Mailbox\"               : 15,\n",
    "    \"Fire Hydrant\"          : 16,\n",
    "    # Static Class Mixed\n",
    "    \"Freestanding Plant\"    : 17,\n",
    "    \"Pole\"                  : 18,\n",
    "    \"Informational Sign\"    : 19,\n",
    "    \"Door\"                  : 20,\n",
    "    \"Fence\"                 : 21,\n",
    "    \"Railing\"               : 22,\n",
    "    \"Cone\"                  : 23,\n",
    "    \"Chair\"                 : 24,\n",
    "    \"Bench\"                 : 25,\n",
    "    \"Table\"                 : 26,\n",
    "    \"Trash Can\"             : 27,\n",
    "    \"Newspaper Dispenser\"   : 28,\n",
    "    # Static Classes Indoor\n",
    "    \"Room Label\"            : 29,\n",
    "    \"Stanchion\"             : 30,\n",
    "    \"Sanitizer Dispenser\"   : 31,\n",
    "    \"Condiment Dispenser\"   : 32,\n",
    "    \"Vending Machine\"       : 33,\n",
    "    \"Emergency Aid Kit\"     : 34,\n",
    "    \"Fire Extinguisher\"     : 35,\n",
    "    \"Computer\"              : 36,\n",
    "    \"Television\"            : 37, # unused\n",
    "    \"Other\"                 : 38,\n",
    "    \"Horse\"                 : 39,\n",
    "    # New Classes\n",
    "    \"Pickup Truck\"          : 40,\n",
    "    \"Delivery Truck\"        : 41,\n",
    "    \"Service Vehicle\"       : 42,\n",
    "    \"Utility Vehicle\"       : 43,\n",
    "    \"Fire Alarm\"            : 44,\n",
    "    \"ATM\"                   : 45,\n",
    "    \"Cart\"                  : 46,\n",
    "    \"Couch\"                 : 47,\n",
    "    \"Traffic Arm\"           : 48,\n",
    "    \"Wall Sign\"             : 49,\n",
    "    \"Floor Sign\"            : 50,\n",
    "    \"Door Switch\"           : 51,\n",
    "    \"Emergency Phone\"       : 52,\n",
    "    \"Dumpster\"              : 53,\n",
    "    \"Vacuum Cleaner\"        : 54, # unused\n",
    "    \"Segway\"                : 55,\n",
    "    \"Bus\"                   : 56,\n",
    "    \"Skateboard\"            : 57,\n",
    "    \"Water Fountain\"        : 58\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "METAINFO = {\n",
    "        'classes':\n",
    "        tuple(BBOX_CLASS_TO_ID.keys()),\n",
    "        'version':\n",
    "        'v1.0-trainval',\n",
    "        'palette': [\n",
    "            (255, 158, 0),  # Orange\n",
    "            (255, 99, 71),  # Tomato\n",
    "            (255, 140, 0),  # Darkorange\n",
    "            (255, 127, 80),  # Coral\n",
    "            (233, 150, 70),  # Darksalmon\n",
    "            (220, 20, 60),  # Crimson\n",
    "            (255, 61, 99),  # Red\n",
    "            (0, 0, 230),  # Blue\n",
    "            (47, 79, 79),  # Darkslategrey\n",
    "            (112, 128, 144),  # Slategrey\n",
    "        ]\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "METAINFO['classes']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ros_perception",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
